package com.ming.yygh.user.api;

import com.ming.yygh.common.result.Result;
import com.ming.yygh.common.utils.AuthContextHolder;
import com.ming.yygh.model.user.Patient;
import com.ming.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Light
 * @date 2021/12/21 11:16
 */
@Api(tags = "就诊人相关接口")
@RestController
@RequestMapping("api/user/patient")
public class PatientApiController {

    @Resource
    private PatientService patientService;

    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("/auth/findAll")
    public Result<List<Patient>> findAll(HttpServletRequest request) {
        // 获取当前用户的id
        String userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllByUserId(userId);
        return Result.ok(list);
    }

    @ApiOperation(value = "添加就诊人")
    @PostMapping("/auth/save")
    public Result<Boolean> savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        // 获取当前登录用户的id
        String userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        boolean save = patientService.save(patient);
        return Result.ok(save);
    }

    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("/auth/get/{id}")
    public Result<Patient> getPatient(@PathVariable(value = "id") String id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    @ApiOperation(value = "修改就诊人信息")
    @PostMapping("/auth/update")
    public Result<Boolean> updatePatient(@RequestBody Patient patient, HttpServletRequest request) {
        boolean save = patientService.updateById(patient);
        return Result.ok(save);
    }

    @ApiOperation(value = "删除就诊人信息")
    @DeleteMapping("/auth/remove/{id}")
    public Result<Boolean> removePatient(@PathVariable(value = "id") String id) {
        boolean success = patientService.removeById(id);
        return Result.ok(success);
    }

    @ApiOperation(value = "根据就诊人id获取就诊人信息")
    @GetMapping("/inner/get/{id}")
    public Result<Patient> getPatientOrder(@PathVariable(value = "id") String id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }
}
